package logic

import (
	"context"
	"fmt"
	"user-profile/model/mysql"
	"user-profile/userProfilerpc/internal/svc"
	"user-profile/userProfilerpc/userProfilerpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserInfoAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserInfoAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserInfoAddLogic {
	return &UserInfoAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserInfoAddLogic) UserInfoAdd(in *userProfilerpc.UserInfoAddRequest) (*userProfilerpc.UserInfoAddResponse, error) {
	// todo: add your logic here and delete this line
	var findUser mysql.User
	mysql.DB.Where("id = ?", in.UserId).Find(&findUser)
	if findUser.ID == 0 {
		return nil, fmt.Errorf("账号未登录,不允许添加")
	}
	info := mysql.UserInfo{
		UserId:      int(in.UserId),
		HeaderImage: in.HeaderImage,
		Name:        in.Name,
		Happy:       in.Happy,
		Sex:         in.Sex,
		Info:        in.Info,
	}
	mysql.DB.Debug().Create(&info)
	return &userProfilerpc.UserInfoAddResponse{
		Id: int64(info.ID),
	}, nil
}
